package com.ohuang.wallpapermanager.config

import android.graphics.BitmapFactory
import com.ohuang.wallpapermanager.util.AppUtil
import com.ohuang.wallpapermanager.util.BitMapUtil
import java.io.File

object LocalImageManager {
    private val imgDir = AppUtil.getInstance().context.filesDir.absolutePath + "/images"

    private fun createImageName(): String {
        return imgDir+"/" + System.currentTimeMillis() + ".png"
    }

    fun saveImageForUri(uri:String):String{
        try {
            AppUtil.getInstance().context.contentResolver?.openInputStream(android.net.Uri.parse(uri))
                ?.use { inputStream ->
                    val decodeStream = BitmapFactory.decodeStream(inputStream)
                    if (decodeStream!=null){
                        val createImageName = createImageName()
                        val saveBitmap = BitMapUtil.saveBitmap(decodeStream, File(createImageName))
                        if (saveBitmap){
                            return createImageName
                        }
                    }
                }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return ""
    }

}